﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using System.Text;

namespace Runda.CMS.Portal.ContractManage
{
    public partial class ContractList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindContractList(1);
            }
        }

        private void BindContractList(int pageindex)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" and CreatedBy = '").Append(base.CurrentUserID).Append("'");
            sb.Append(" and Status > ").Append((int)Enumerator.ContractStatus.Draft - 1);
 
            if (txtContractCodeBasic.Text.Trim() != string.Empty)
            {
                sb.Append(" and ContractCode like '%").Append(txtContractCodeBasic.Text).Append("%'");
            }

            if (txtCumtomerName.Text.Trim() != string.Empty)
            {
                sb.Append(" and CustomerName like '%").Append(txtCumtomerName.Text.Trim()).Append("%'");
            }

            if (txtInstrumentName.Text.Trim() != string.Empty)
            {
                sb.Append(" and ContractID in (select ContractID from Cms_Ctrt_ContractItem Item JOIN Cms_Ctrt_Instrument Inst ON item.InstrumentGUID = Inst.InstrumentGUID where Inst.InstrumentName like '%").Append(txtInstrumentName.Text.Trim()).Append("%')");
            }

            int RecordCount = 0;
            DataTable dt = new ContractBll().QueryContract(sb.ToString(), "createdon desc", AspNetPager1.PageSize, pageindex, out RecordCount);
                 
            AspNetPager1.RecordCount = RecordCount;
            gridContractList.DataSource = dt;
            gridContractList.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindContractList(1);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindContractList(AspNetPager1.CurrentPageIndex);
        }

        public string GetStatusText(int status)
        { 
            string text = string.Empty;
            EnumeratorHelper[] cs = EnumeratorHelper.GetFieldTexts(typeof(Enumerator.ContractStatus));
            foreach (EnumeratorHelper c in cs)
            {
                if (c.EnumValue == status)
                {
                    text = c.EnumDisplayText; break;
                }
            }

            return text;
        }

        protected void RepeaterContract_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindContractList(1);
            }
        }

        protected void delete(object sender, EventArgs e)
        {
            //if (contractids.Value.Trim() != "")
            //{
            //    string[] contractidsArray = contractids.Value.Trim().Split(new char[] { ';' });
            //    for (int index = 0; index < contractidsArray.Length; index++)
            //    {
            //        CtrtContractBll bll = new CtrtContractBll();
            //        CtrtContractEntity contractEntity = bll.Get<CtrtContractEntity>(new Guid(contractidsArray[index]));
            //        if (contractEntity != null)
            //        {
            //            if (contractEntity.Status == System.Convert.ToInt32(Enumerator.ContractStatus.Draft))
            //            {
            //                bll.LogicDelete(contractEntity);                      
            //            }
            //            else
            //            {
            //                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('该合同已进入流程审批，不能删除!');", true);
            //            }
            //        }
            //    }
            //    BindContractList(1);
            //}

            foreach (Telerik.WebControls.GridDataItem item in gridContractList.Items)
            {
                if (item.ItemType == Telerik.WebControls.GridItemType.Item || item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
                {
                    CheckBox cbxSelect = item.FindControl("cbxSelect") as CheckBox; 
                    if (cbxSelect.Checked)
                    {
                        CtrtContractBll bll = new CtrtContractBll();
                        bll.LogicDelete(new CtrtContractEntity(new Guid(item.OwnerTableView.DataKeyValues[item.ItemIndex]["ContractID"].ToString())));
                    }
                }
            }
            
            BindContractList(1);
        }

  

        protected void gridContractList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                DataRowView dv = e.Item.DataItem as DataRowView;
                if (dv != null)
                {
                    CheckBox cbxSelect = e.Item.FindControl("cbxSelect") as CheckBox;
                    if (Convert.ToInt32(dv["Status"].ToString()) != (int)Enumerator.ContractStatus.Draft)
                    {
                        cbxSelect.Enabled = false;
                    }

                    Label lblContractType = e.Item.FindControl("lblContractType") as Label;
                    if (dv["ContractType"] != DBNull.Value)
                    {
                        lblContractType.Text = EnumeratorHelper.GetFieldText((Enumerator.ContractType)Convert.ToInt32(dv["ContractType"].ToString()));
                    }

                    LinkButton lnkbtnContractCode = e.Item.FindControl("lnkbtnContractCode") as LinkButton;
                    if (lnkbtnContractCode != null)
                    {
                        lnkbtnContractCode.Text = dv["ContractCode"].ToString();
                        if (Convert.ToInt32(dv["Status"].ToString()) == (int)Enumerator.ContractStatus.Draft || Convert.ToInt32(dv["Status"].ToString()) == (int)Enumerator.ContractStatus.Back)
                        {
                            lnkbtnContractCode.Attributes.Add("onclick", "openMyFullWin('ContractAdd.aspx?ContractID=" + dv["ContractID"].ToString() + "', 'editcontract');");
                        }
                        else
                        {
                            lnkbtnContractCode.Attributes.Add("onclick", "openMyFullWin('ContractView.aspx?ContractID=" + dv["ContractID"].ToString()+"', 'editcontract');");
                        }
                    }

                    LinkButton lnkbtnChange = e.Item.FindControl("lnkbtnChange") as LinkButton;
                    if (lnkbtnChange != null)
                    {
                        if (Convert.ToInt32(dv["Status"].ToString()) == (int)Enumerator.ContractStatus.Pass)
                        {

                            lnkbtnChange.Attributes.Add("onclick", "openMyFullWin('ContractChange.aspx?OrginalContractID=" + dv["ContractID"].ToString() + "', 'changecontract');");
                        }
                        else {
                            lnkbtnChange.Visible = false;

                        }
                    }

                    Label lblContractStatement = e.Item.FindControl("lblContractStatement") as Label;
                    if (lblContractStatement != null)
                    {
                        if (dv["ContractStatement"] != null && dv["ContractStatement"] != DBNull.Value)
                        {
                            lblContractStatement.Text = dv["ContractStatement"].ToString().FixLengthString(30);

                            lblContractStatement.ToolTip = dv["ContractStatement"].ToString();
                        }
                    }
                }
            }
        }
    }
}
